package org.gwh.airagknowledge.repository;

import org.gwh.airagknowledge.entity.VectorEmbedding;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface VectorEmbeddingRepository extends JpaRepository<VectorEmbedding, Long> {
    
    @Query(value = 
        "SELECT v.* FROM vector_embedding v " +
        "JOIN document_chunk c ON v.chunk_id = c.id " +
        "JOIN document d ON c.document_id = d.id " +
        "WHERE d.kb_id = :knowledgeBaseId " +
        "ORDER BY v.embedding <-> :queryEmbedding LIMIT :limit", 
        nativeQuery = true)
    List<VectorEmbedding> findSimilarEmbeddings(
        @Param("knowledgeBaseId") Long knowledgeBaseId,
        @Param("queryEmbedding") byte[] queryEmbedding,
        @Param("limit") int limit);

    void deleteByChunkId(Long chunkId);
} 